package com.dtp.core.parser;

import com.dtp.common.em.ConfigFileTypeEnum;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Yaml类型配置文件解析类
 *
 * @author yanhom
 * @since 1.0.0
 *
 * @remark muse
 **/
public class YamlConfigParser extends AbstractConfigParser {
    private static final List<ConfigFileTypeEnum> CONFIG_TYPES = Lists.newArrayList(
            ConfigFileTypeEnum.YML, ConfigFileTypeEnum.YAML);

    /** 该解析类支持的解析类型为：YML和YAML */
    @Override
    public List<ConfigFileTypeEnum> types() {return CONFIG_TYPES;}

    @Override
    public Map<Object, Object> doParse(String content) {
        if (StringUtils.isEmpty(content)) return Collections.emptyMap();

        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ByteArrayResource(content.getBytes()));
        return bean.getObject();
    }
}
